//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：UIFrameViewAssets
// *描述：用于加载/销毁资源的默认脚本。
// *?2021 MYJL . All rights reserved.
//* ======================================================================
using System;
using UnityEngine;

namespace HDMCode.UIFrame
{
    public class UIFrameViewAssets : IUIFrameViewAssets
    {
        /// <summary>
        /// 加载UI
        /// </summary>
        public bool LoadUI(string assetsName, ref UIDisplay UI)
        {
#if (com_tuyoogame_yooasset)
             try
            {
                 GameObject uiAssets = Hotfix.HDMAssetsLoad.LoadGameObject(assetsName, HDMUI.Runtime.Canvas.transform);
                 uiAssets.SetActive(false);
                 uiAssets.name = assetsName;
                 UI = uiAssets.GetComponent<UIDisplay>();
                 HDMUI.Runtime.FrameStore.TryAddToStore(UI);
                 return true;
             }
             catch (Exception error)
             {
                 Debug.LogWarning($" UIFrame UI加载失败！ -- UI：{assetsName}！当前模式只能通过Resources加载！" + error);
                 return false;
             }
#else
             try
             {
                 GameObject uiAssets = (GameObject)Resources.Load(assetsName);
                 GameObject laodGameObject = GameObject.Instantiate((GameObject)Resources.Load(assetsName), HDMUI.Runtime.Canvas.transform);
                 laodGameObject.name = uiAssets.name;
                 UI = laodGameObject.GetComponent<UIDisplay>();
                 HDMUI.Runtime.FrameStore.TryAddToStore(UI);
                 laodGameObject.gameObject.SetActive(true);
                 return true;
             }
             catch (Exception error)
             {
                 Debug.LogWarning($" UIFrame UI加载失败！ -- UI：{assetsName}！当前模式只能通过Resources加载！" + error);
                 return false;
             }
#endif
        }

        /// <summary>
        /// 销毁UI
        /// </summary>
        public void DestroyUI(string uiName, UIDisplay UI)
        {

#if (com_tuyoogame_yooasset)
            HDMCode.Hotfix.HDMAssetsLoad.TryUnloadUnusedAsset(uiName);
            if (UI.gameObject)
                GameObject.Destroy(UI.gameObject);
#else
            GameObject.Destroy(UI.gameObject);
#endif
        }
    }
}